home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <dtypes.h>
- #include <string.h>
- #include <alloc.h>
- #include "code.h"
- #include "host.h"
- #include "newload.h"
- #include "callasm.h"
-
- PSTR derror[] = {
- "COMPILER ERROR",
- "Symbol exists",
- "Symbol table overflow",
- "Symbol undefined",
- "Syntax error",
- "Symbol required as first argument",
- "Label already used",
- "Line reference not allowed as arg",
- "Line reference required",
- "Too few/many arguments supplied",
- "Out of RAM",
- ""
- };
-
- #define _SYMUSED 1
- #define _VAROVER 2
- #define _UNDEF 3
- #define _SYNTAX 4
- #define _SYMREQ1 5
- #define _LABELUSED 6
- #define _NOLINE 7
- #define _LINEMUST 8
- #define _WRONGARGS 9
- #define _NORAM 10
-
- p_RESOLVE first_resolve;
- p_RESOLVE last_resolve;
- p_RESOLVE cresolve;
- p_LINER lineref_start;
- p_LINER lineref_end;
- p_LINE lastline;
- p_LINE cl;
- p_SYMB first_symbol;
- p_SYMB last_symbol;
- TANK tanks[MAXPLAYERS];
- int nplayers;
- int lerror;
- BOOL appendcl;
- int num_symb;
- BOOL bad_file;
- BOOL noramleft;
- TOKEN maketoken;
-
- /*<f>----------------------------------------
- * FUNCTION: <s> int n_compile_player(p_PLAYER p)
- * PURPOSE : Compile a player and return the handle to it's structure
- * :
- * CREATION: 01/06/1989 09:45:51
- */
- int n_compile_player(p_PLAYER p)
- {
- int hand;
-
- noramleft=FALSE;
-
- hand=create_new_player(p->tankname);
-
- compile_program(&tanks[hand]);
-
- if (bad_file)
- hand=-1;
-
- free_compiler_ram();
-
- #ifdef DEBUG
- list_program_structure(&tanks[hand]);
- #endif
-
- if (noramleft)
- hand=-2;
-
- return hand;
- } /* int n_compile_player(p_PLAYER p) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void init_structures(void)
- * PURPOSE : Initilize all used data structures to known values
- * :
- * CREATION: 12/14/1988 08:11:38
- */
- void init_structures(void)
- {
- nplayers=0;
- } /* void init_structures(void) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> int create_new_player(PSTR s)
- * PURPOSE : Initilize a new players tank structure
- * :
- * CREATION: 12/14/1988 08:12:43
- */
- int create_new_player(PSTR s)
- {
- if (nplayers==MAXPLAYERS)
- return BAD;
-
- strcpy(tanks[nplayers].basefilename,s);
- tanks[nplayers].prog=(0L);
-
- nplayers++;
- return (nplayers-1);
- } /* int create_new_player(PSTR s) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void compile_program(p_TANK t)
- * PURPOSE : Load in tank logic program and compile it into
- * : interpreter usable code.
- * CREATION: 12/14/1988 08:27:50
- */
- void compile_program(p_TANK t)
- {
- char fn[60];
- FILE *fp;
- char ins[255], lcopy[255], *p;
- int pl,a;
- BOOL MakeNewLine;
- BOOL ferror, stopit;
- char err[50];
-
- strcpy(fn,t->basefilename);
- strcat(fn,".TXT");
-
- if (!exist(fn)) {
- bad_file=TRUE;
- note_error("FILE NOT FOUND");
- } else {
- fp=fopen(fn,"rt");
-
- pl = 0;
- num_symb = 0;
- lastline = 0L;
- MakeNewLine = TRUE;
- ferror = FALSE;
- lineref_end = lineref_start = NULL;
- first_resolve= last_resolve = NULL;
- first_symbol = last_symbol = NULL;
-
- create_token(&maketoken,"WHEREX");
- add_symbol(&maketoken);
- create_token(&maketoken,"WHEREY");
- add_symbol(&maketoken);
- create_token(&maketoken,"CGDIR");
- add_symbol(&maketoken);
- create_token(&maketoken,"CTDIR");
- add_symbol(&maketoken);
- create_token(&maketoken,"LOADED");
- add_symbol(&maketoken);
- create_token(&maketoken,"HIT");
- add_symbol(&maketoken);
- create_token(&maketoken,"NOMOVE");
- add_symbol(&maketoken);
- create_token(&maketoken,"TEAM");
- add_symbol(&maketoken);
- create_token(&maketoken,"MYTEAM");
- add_symbol(&maketoken);
- create_token(&maketoken,"BLOCKAGE");
- add_symbol(&maketoken);
- create_token(&maketoken,"MESSAGE");
- add_symbol(&maketoken);
-
-
- stopit=FALSE;
-
- while (fgets(ins,255,fp) && (!stopit)) {
- pl++;
- strcpy(lcopy,ins);
- lcopy[strlen(lcopy)-1]=' ';
-
- /* Erase everything from a ";" onwards */
- p=ins;
- while ((*p)!=';' && (*p)) p++;
- if ((*p)==';') (*p)=NULL;
-
- lerror=0;
-
- if (MakeNewLine) {
- #ifdef DEBUG
- printf("-> Creating newline for line %s\n",lcopy);
- #endif
- cl=create_newline();
- nuke_line_contents(cl);
- }
-
- cl->pline=pl;
- appendcl=TRUE;
-
- parse_line(ins);
-
- switch (lerror) {
- case 0 : /* No errors in this line, add it to array if required */
- if (appendcl) {
- add_line(t,cl);
- MakeNewLine=TRUE;
- } else MakeNewLine=FALSE;
- break;
-
- default : sprintf(err,"%s L%d",derror[lerror],pl);
- note_error(err);
- sprintf(err,"- %s",lcopy);
- note_error(err);
-
- ferror=TRUE;
- MakeNewLine=FALSE;
- if (lerror==_NORAM) {
- stopit=TRUE;
- noramleft=TRUE;
- }
- break;
- }
- }
-
- fclose(fp);
-
- bad_file=FALSE;
- if (!ferror)
- resolve_references(t);
- else
- bad_file=TRUE;
- }
- } /* void compile_program(p_TANK t) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> BOOL exist(PSTR s)
- * PURPOSE : Returns TRUE is file exists, FALSE otherwise
- * :
- * CREATION: 12/14/1988 08:49:28
- */
- BOOL exist(PSTR s)
- {
- FILE *fp;
-
- if ((fp=fopen(s,"rt"))==NULL) return FALSE;
-
- fclose(fp);
- return TRUE;
- } /* BOOL exist(PSTR s) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void nuke_line_contents(p_LINE l)
- * PURPOSE : Clear out a line structure to a known value
- * :
- * CREATION: 12/14/1988 08:57:29
- */
- void nuke_line_contents(p_LINE l)
- {
- l->pline=BAD;
- l->command=BAD;
-
- nuke_argument(&l->arg1);
- nuke_argument(&l->arg2);
- nuke_argument(&l->arg3);
- nuke_argument(&l->arg4);
-
- } /* void nuke_line_contents(p_LINE l) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void nuke_argument(p_ARG a)
- * PURPOSE : Clear out a argument structure to a known value
- * :
- * CREATION: 12/14/1988 08:59:29
- */
- void nuke_argument(p_ARG a)
- {
- a->atype=BAD;
- a->value=BAD;
- a->linedest=NULL;
- } /* void nuke_argument(p_ARG a) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void parse_line(PSTR l)
- * PURPOSE : Parse out a text line into structure elements
- * :
- * CREATION: 12/14/1988 09:03:38
- */
- void parse_line(PSTR l)
- {
- TOKEN t[MAXTOKENS];
- PSTR p;
- int nt;
-
- strupr(l); /* Make everything UPPER-CASE */
-
- memset(t,0,sizeof(t));
-
- p=strtok(l," ,\n\t");
- nt=0;
-
- while (nt<MAXTOKENS && p!=NULL) {
- strcpy(t[nt++].token,p);
- p = strtok(NULL," ,\n\t");
- }
-
- if (nt>0)
- fill_structure(t,nt-1);
- else {
- lerror=0;
- appendcl=FALSE;
- }
- } /* void parse_line(PSTR l) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void fill_structure(TOKEN t[], int nt)
- * PURPOSE : Fill Current Line structure (CL) with
- * : information from supplied token list
- * CREATION: 12/14/1988 09:20:01
- */
- void fill_structure(TOKEN t[], int nt)
- {
- int cmd, na;
-
- if (t[0].token[0]==':') {
- add_lineref(&t[0]);
- appendcl=FALSE;
- return;
- }
-
- cmd=BAD;
- if (strcmp(t[0].token,"MAKE" )==0) { cmd=_MAKE ; na=1; }
- if (strcmp(t[0].token,"AUTO" )==0) { cmd=_AUTO ; na=1; }
- if (strcmp(t[0].token,"LOCK" )==0) { cmd=_LOCK ; na=1; }
- if (strcmp(t[0].token,"CHANNEL")==0) { cmd=_CHANNEL; na=1; }
- if (strcmp(t[0].token,"TX" )==0) { cmd=_TX ; na=3; }
- if (strcmp(t[0].token,"RX" )==0) { cmd=_RX ; na=3; }
- if (strcmp(t[0].token,"CONV" )==0) { cmd=_CONV ; na=3; }
- if (strcmp(t[0].token,"JAMMER" )==0) { cmd=_JAMMER ; na=1; }
- if (strcmp(t[0].token,"RADAR" )==0) { cmd=_RADAR ; na=1; }
- if (strcmp(t[0].token,"SCAN" )==0) { cmd=_SCAN ; na=2; }
- if (strcmp(t[0].token,"FACE" )==0) { cmd=_FACE ; na=1; }
- if (strcmp(t[0].token,"AIM" )==0) { cmd=_AIM ; na=1; }
- if (strcmp(t[0].token,"SELECT" )==0) { cmd=_SELECT ; na=1; }
- if (strcmp(t[0].token,"SCOPE" )==0) { cmd=_SCOPE ; na=1; }
- if (strcmp(t[0].token,"JEQ" )==0) { cmd=_JEQ ; na=3; }
- if (strcmp(t[0].token,"JLT" )==0) { cmd=_JLT ; na=3; }
- if (strcmp(t[0].token,"JNEQ" )==0) { cmd=_JNEQ ; na=3; }
- if (strcmp(t[0].token,"JGT" )==0) { cmd=_JGT ; na=3; }
- if (strcmp(t[0].token,"LEFT" )==0) { cmd=_LEFT ; na=0; }
- if (strcmp(t[0].token,"RIGHT" )==0) { cmd=_RIGHT ; na=0; }
- if (strcmp(t[0].token,"GLEFT" )==0) { cmd=_GLEFT ; na=0; }
- if (strcmp(t[0].token,"GRIGHT" )==0) { cmd=_GRIGHT ; na=0; }
- if (strcmp(t[0].token,"SET" )==0) { cmd=_SET ; na=2; }
- if (strcmp(t[0].token,"ADD" )==0) { cmd=_ADD ; na=2; }
- if (strcmp(t[0].token,"RAND" )==0) { cmd=_RAND ; na=2; }
- if (strcmp(t[0].token,"MOVE" )==0) { cmd=_MOVE ; na=0; }
- if (strcmp(t[0].token,"GOTO" )==0) { cmd=_GOTO ; na=1; }
- if (strcmp(t[0].token,"FIRE" )==0) { cmd=_FIRE ; na=0; }
- if (strcmp(t[0].token,"CALL" )==0) { cmd=_CALL ; na=1; }
- if (strcmp(t[0].token,"RETURN" )==0) { cmd=_RETURN ; na=0; }
-
- if (cmd==BAD) {
- lerror=_SYNTAX;
- return;
- }
-
- if (na!=nt) {
- lerror=_WRONGARGS;
- return;
- }
-
- if (cmd==_MAKE) {
- add_symbol(&t[1]);
- appendcl=FALSE;
- return;
- }
-
- cl->command=cmd;
-
- switch (cmd) {
- case _SCOPE :
- case _RADAR : fill_s(&t[1]); break;
-
- case _SET :
- case _ADD :
- case _RAND :
- case _SCAN : fill_sa(&t[1],&t[2]); break;
-
- case _AUTO :
- case _LOCK :
- case _JAMMER :
- case _CHANNEL:
- case _FACE :
- case _AIM :
- case _SELECT : fill_a(&t[1]); break;
-
- case _JLT :
- case _JNEQ :
- case _JGT :
- case _JEQ : fill_aal(&t[1],&t[2],&t[3]); break;
-
- case _TX : fill_aaa(&t[1],&t[2],&t[3]); break;
-
- case _RX : fill_sss(&t[1],&t[2],&t[3]); break;
-
- case _CONV : fill_aas(&t[1],&t[2],&t[3]); break;
-
- case _GOTO :
- case _CALL : fill_l(&t[1]); break;
- }
-
- } /* void fill_structure(TOKEN t[], int nt) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void fill_l(p_TOKEN t1)
- * PURPOSE : Fill Current Line structure with a LINEREF command
- * :
- * CREATION: 12/15/1988 10:27:49
- */
- void fill_l(p_TOKEN t1)
- {
- if (!parse_arg(&cl->arg1, t1))
- lerror=_SYNTAX;
- else if (cl->arg1.atype!=LINEREF)
- lerror=_LINEMUST;
- } /* void fill_l(p_TOKEN t1) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void fill_sa(p_TOKEN t1,p_TOKEN t2)
- * PURPOSE : Fill CL structure with SYMBOL,ANYTHING command
- * :
- * CREATION: 12/15/1988 10:31:35
- */
- void fill_sa(p_TOKEN t1,p_TOKEN t2)
- {
- if (!parse_arg(&cl->arg1, t1) || !parse_arg(&cl->arg2, t2))
- lerror=_SYNTAX;
- else if (cl->arg1.atype!=SYMBOL)
- lerror=_SYMREQ1;
- else if (cl->arg2.atype==LINEREF)
- lerror=_NOLINE;
- } /* void fill_sa(p_TOKEN t1,p_TOKEN t2) */
-
- /*-------------------------------------
- * Function : void fill_sss(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- * Purpose : Fill CL structure with SYMBOL, SYMBOL, SYMBOL command
- * Date : 02/13/1989 22:28:53
- */
- void fill_sss(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- {
- if (!parse_arg(&cl->arg1, t1) ||
- !parse_arg(&cl->arg2, t2) ||
- !parse_arg(&cl->arg3, t3))
- lerror=_SYNTAX;
- else if (cl->arg1.atype!=SYMBOL || cl->arg2.atype!=SYMBOL || cl->arg3.atype!=SYMBOL)
- lerror=_SYMREQ1;
- } /* void fill_sss(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3) */
-
- /*-------------------------------------
- * Function : void fill_aas(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- * Purpose : Fill CL structure with ANYTHING, ANYTHING, SYMBOL command
- * Date : 02/13/1989 22:28:53
- */
- void fill_aas(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- {
- if (!parse_arg(&cl->arg1, t1) ||
- !parse_arg(&cl->arg2, t2) ||
- !parse_arg(&cl->arg3, t3))
- lerror=_SYNTAX;
- else if (cl->arg1.atype==LINEREF || cl->arg2.atype==LINEREF)
- lerror = _NOLINE;
- else if (cl->arg3.atype!=SYMBOL)
- lerror=_SYMREQ1;
- } /* void fill_aas(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3) */
-
- /*-------------------------------------
- * Function : void fill_aaa(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- * Purpose : Fill CL structure with ANYTHING, ANYTHING, ANYTHING command
- * Date : 02/13/1989 22:32:18
- */
- void fill_aaa(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- {
- if (!parse_arg(&cl->arg1, t1) ||
- !parse_arg(&cl->arg2, t2) ||
- !parse_arg(&cl->arg3, t3))
- lerror=_SYNTAX;
- else if (cl->arg1.atype==LINEREF || cl->arg2.atype==LINEREF || cl->arg3.atype==LINEREF)
- lerror=_NOLINE;
-
- } /* void fill_aaa(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void fill_aal(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- * PURPOSE : Fill CL structure with ANYTHING, ANYTHING, LINEREF command
- * :
- * CREATION: 12/15/1988 10:34:46
- */
- void fill_aal(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3)
- {
- if (!parse_arg(&cl->arg1, t1) ||
- !parse_arg(&cl->arg2, t2) ||
- !parse_arg(&cl->arg3, t3))
- lerror=_SYNTAX;
- else if (cl->arg1.atype==LINEREF || cl->arg2.atype==LINEREF)
- lerror=_NOLINE;
- else if (cl->arg3.atype!=LINEREF)
- lerror=LINEREF;
- } /* void fill_aal(p_TOKEN t1, p_TOKEN t2, p_TOKEN t3) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void fill_a(p_TOKEN t1)
- * PURPOSE : Fill CL structure with ANYTHING command
- * :
- * CREATION: 12/15/1988 10:38:19
- */
- void fill_a(p_TOKEN t1)
- {
- if (!parse_arg(&cl->arg1,t1))
- lerror=_SYNTAX;
- else if (cl->arg1.atype==LINEREF)
- lerror=_NOLINE;
- } /* void fill_a(p_TOKEN t1) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void fill_s(p_TOKEN t1)
- * PURPOSE : Fill CL structure with SYMBOL command
- * :
- * CREATION: 12/15/1988 10:39:29
- */
- void fill_s(p_TOKEN t1)
- {
- if (!parse_arg(&cl->arg1,t1))
- lerror=_SYNTAX;
- else if (cl->arg1.atype!=SYMBOL)
- lerror=_SYMREQ1;
- } /* void fill_s(p_TOKEN t1) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> BOOL parse_arg(p_ARG a, p_TOKEN t)
- * PURPOSE : Parse out token T into argument A
- * :
- * CREATION: 12/15/1988 10:42:03
- */
- BOOL parse_arg(p_ARG a, p_TOKEN t)
- {
- if (!t->token) return FALSE;
-
- if (t->token[0]==':') {
- a->atype=LINEREF;
- if ((a->linedest=find_lineref(t))==NULL)
- add_resolve(t);
- return TRUE;
- } else if ((t->token[0]=='-') || (t->token[0]>='0' && t->token[0]<='9')) {
- a->atype=CONSTANT;
- a->value=atoi(t->token);
- return TRUE;
- }
- a->atype=SYMBOL;
- a->value=find_symbol(t);
- return (a->value==BAD) ? FALSE : TRUE;
- } /* BOOL parse_arg(p_ARG a, p_TOKEN t) */
-
- /*-------------------------------------
- * Function : void add_lineref(p_TOKEN t)
- * Purpose : Add's a new line reference from token T
- * Date : 12/15/1988 14:19:38
- */
- void add_lineref(p_TOKEN t)
- {
- p_LINER newliner;
-
- newliner=(p_LINER) malloc(sizeof(LINER));
-
- if (newliner==NULL) {
- lerror=_NORAM;
- return;
- }
-
- newliner->next_liner=NULL;
- newliner->lineref =cl;
- strcpy(newliner->lsymbol.token,t->token);
-
- if (lineref_end==NULL)
- lineref_start=newliner;
- else lineref_end->next_liner=newliner;
-
- lineref_end=newliner;
-
- } /* void add_lineref(p_TOKEN t) */
-
- /*-------------------------------------
- * Function : p_LINE find_lineref(p_TOKEN t)
- * Purpose : Search line reference file for token T and return pointer to line
- * Date : 12/15/1988 14:44:37
- */
- p_LINE find_lineref(p_TOKEN t)
- {
- p_LINER rec;
-
- for (rec=lineref_start; rec!=NULL; rec=rec->next_liner)
- if (strcmp(rec->lsymbol.token,t->token)==0)
- return rec->lineref;
-
- return (0L);
-
- } /* p_LINE find_lineref(p_TOKEN t) */
-
- /*-------------------------------------
- * Function : int find_symbol(p_TOKEN t);
- * Purpose : Search symbol table for token T
- * Date : 12/15/1988 15:22:56
- */
- int find_symbol(p_TOKEN t)
- {
- p_SYMB rec;
-
- for (rec=first_symbol; rec!=NULL; rec=rec->next_symbol)
- if (strcmp(rec->symbol.token,t->token)==0)
- return rec->memloc;
-
- return BAD;
-
- } /* int find_symbol(p_TOKEN t); */
-
- /*-------------------------------------
- * Function : void add_line(p_TANK t, p_LINE l)
- * Purpose : Adds new line L to tank structure T
- * Date : 12/15/1988 14:22:24
- */
- void add_line(p_TANK t, p_LINE l)
- {
- l->nextline=(0L);
-
- if (lastline==(0L))
- t->prog=l;
- else lastline->nextline=l;
-
- lastline=l;
- } /* void add_line(p_TANK t, p_LINE l) */
-
- /*-------------------------------------
- * Function : p_LINE create_newline(void)
- * Purpose : Gets ram for a new line structure
- * Date : 12/15/1988 14:30:20
- */
- p_LINE create_newline(void)
- {
- p_LINE newline;
-
- newline=(p_LINE) farmalloc(sizeof(LINE));
- if (newline==(0L))
- lerror=_NORAM;
- return newline;
- } /* p_LINE create_newline(void) */
-
- /*-------------------------------------
- * Function : void add_resolve(p_TOKEN t)
- * Purpose : Add a new lineref to resolve later
- * Date : 12/15/1988 14:52:52
- */
- void add_resolve(p_TOKEN t)
- {
- p_RESOLVE newres;
-
- newres=(p_RESOLVE) malloc(sizeof(RESOLVE));
- if (newres==NULL) {
- lerror=_NORAM;
- return;
- }
-
- newres->next_resolve=NULL;
- strcpy(newres->lineref.token,t->token);
-
- if (last_resolve==NULL)
- first_resolve=newres;
- else last_resolve->next_resolve=newres;
-
- last_resolve=newres;
-
- } /* void add_resolve(p_TOKEN t) */
-
- /*-------------------------------------
- * Function : void add_symbol(p_TOKEN t)
- * Purpose : Add's new symbol to symbol table
- * Date : 12/15/1988 15:15:50
- */
- void add_symbol(p_TOKEN t)
- {
- p_SYMB news;
-
- news=(p_SYMB) malloc(sizeof(SYMB));
- if (news==NULL) {
- lerror=_NORAM;
- return;
- }
-
- news->next_symbol=NULL;
- strcpy(news->symbol.token,t->token);
- news->memloc = num_symb++;
-
- if (last_symbol==NULL)
- first_symbol=news;
- else last_symbol->next_symbol=news;
-
- last_symbol=news;
- } /* void add_symbol(p_TOKEN t) */
-
- /*-------------------------------------
- * Function : void list_symbol_table(void)
- * Purpose : List symbol table to screen for debugging
- * Date : 12/15/1988 15:59:00
- */
- void list_symbol_table(void)
- {
- p_SYMB rec;
-
- printf("\n-----------------\nSYMBOLS DEFINED\n\n");
-
- for (rec=first_symbol; rec!=NULL; rec=rec->next_symbol)
- printf("SYMBOL : %s \n",rec->symbol.token);
-
- } /* void list_symbol_table(void) */
-
- /*-------------------------------------
- * Function : void resolve_references(p_TANK t)
- * Purpose : Index undefined line references to correct line, if any
- * Date : 12/15/1988 16:20:36
- */
- void resolve_references(p_TANK t)
- {
- p_LINE rec;
-
- cresolve=first_resolve;
-
- for (rec=t->prog; rec!=NULL; rec=rec->nextline) {
- check_link(&rec->arg1, rec->pline);
- check_link(&rec->arg2, rec->pline);
- check_link(&rec->arg3, rec->pline);
- }
- } /* void resolve_references(p_TANK t) */
-
- /*-------------------------------------
- * Function : void check_link(p_ARG a, int pl)
- * Purpose : Check if an argument is resolved, do so if not
- * Date : 12/15/1988 16:24:48
- */
- void check_link(p_ARG a, int pl)
- {
- char buf[100];
- if (a->atype!=LINEREF || a->linedest!=NULL)
- return;
-
- a->linedest=find_lineref(&cresolve->lineref);
-
- if (a->linedest==NULL) {
- sprintf(buf,"Undefined line reference \"%s\" in line %d\n\r",cresolve->lineref.token,pl);
- note_error(buf);
- bad_file=TRUE;
- }
-
- if (cresolve->next_resolve!=NULL)
- cresolve=cresolve->next_resolve;
-
- } /* void check_link(p_ARG a, int pl) */
-
- /*-------------------------------------
- * Function : void free_compiler_ram(void)
- * Purpose : Free up symbol table and resolve table ram
- * Date : 12/15/1988 16:49:35
- */
- void free_compiler_ram(void)
- {
- p_RESOLVE res;
- p_RESOLVE tres;
-
- p_SYMB sym;
- p_SYMB tsym;
-
- res=first_resolve;
- while (res!=NULL) {
- tres=res;
- res=res->next_resolve;
- free(tres);
- }
- first_resolve=NULL;
-
- sym=first_symbol;
- while (sym!=NULL) {
- tsym=sym;
- sym=sym->next_symbol;
- free(tsym);
- }
- first_symbol=NULL;
-
- } /* void free_compiler_ram(void) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void list_program_structure(p_TANK t)
- * PURPOSE : List program structure
- * :
- * CREATION: 01/08/1989 14:22:23
- */
- void list_program_structure(p_TANK t)
- {
- p_LINE rec;
-
- rec=t->prog;
-
- while (rec!=(0L)) {
- explain_line(rec);
- printf("\n");
- rec=rec->nextline;
- }
- } /* void list_program_structure(p_TANK t) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void explain_line(p_LINE l)
- * PURPOSE : What does line L contain
- * :
- * CREATION: 01/08/1989 15:08:56
- */
- void explain_line(p_LINE l)
- {
- char cmd[20];
-
- switch(l->command) {
- case BAD : strcpy(cmd,"BAD"); break;
- case _LEFT : strcpy(cmd,"LEFT"); break;
- case _JLT : strcpy(cmd,"JLT"); break;
- case _JGT : strcpy(cmd,"JGT"); break;
- case _GOTO : strcpy(cmd,"GOTO"); break;
- case _JEQ : strcpy(cmd,"JEQ"); break;
- case _JNEQ : strcpy(cmd,"JNEQ"); break;
- case _CALL : strcpy(cmd,"CALL"); break;
- case _MOVE : strcpy(cmd,"MOVE"); break;
- case _GLEFT : strcpy(cmd,"GLEFT"); break;
- case _FIRE : strcpy(cmd,"FIRE"); break;
- case _RETURN : strcpy(cmd,"RETURN"); break;
- case _SET : strcpy(cmd,"SET"); break;
- case _ADD : strcpy(cmd,"ADD"); break;
- case _RAND : strcpy(cmd,"RAND"); break;
- case _RIGHT : strcpy(cmd,"RIGHT"); break;
- case _GRIGHT : strcpy(cmd,"GRIGHT"); break;
- case _SELECT : strcpy(cmd,"SELECT"); break;
- case _RADAR : strcpy(cmd,"RADAR"); break;
- case _SCAN : strcpy(cmd,"SCAN"); break;
- case _MAKE : strcpy(cmd,"MAKE"); break;
- }
-
- printf("[%2d. %s] ",l->pline,cmd);
-
- explain_arg(&l->arg1);
- explain_arg(&l->arg2);
- explain_arg(&l->arg3);
-
- } /* void explain_line(p_LINE l) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void explain_arg(p_ARG a)
- * PURPOSE : Explain what arg A does or contains
- * :
- * CREATION: 01/08/1989 15:12:13
- */
- void explain_arg(p_ARG a)
- {
- switch(a->atype) {
- case CONSTANT : printf("[%d] ",a->value); break;
- case SYMBOL : printf("<%d> ",a->value); break;
- case LINEREF : explain_line(a->linedest); break;
- case BAD : printf(" * "); break;
- }
- } /* void explain_arg(p_ARG a) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> p_TANK get_tank_pointer(int n)
- * PURPOSE : Returns pointer to tank structure N
- * :
- * CREATION: 01/09/1989 06:51:41
- */
- p_TANK get_tank_pointer(int n)
- {
- return (&tanks[n]);
- } /* p_TANK get_tank_pointer(int n) */
-
- /*<f>----------------------------------------
- * FUNCTION: <s> void create_token(p_TOKEN t, PSTR s)
- * PURPOSE : Create a token T from string s
- * :
- * CREATION: 01/12/1989 08:06:13
- */
- void create_token(p_TOKEN t, PSTR s)
- {
- strcpy(t->token,s);
- } /* void create_token(p_TOKEN t, PSTR s) */